namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Entities.Modules.Definitions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Xml;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The PaDnnAdapter_V2Provider extends PaDnnAdapterBase to support V2 Providers
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	04/21/2005  documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class PaDnnAdapter_V2Provider : PaDnnAdapterBase
    {
        public PaDnnAdapter_V2Provider(PaInstallInfo InstallerInfo) : base(InstallerInfo)
        {
        }

        public override PaFolderCollection ReadDnn()
        {
            IEnumerator refObjectHelperL1=null;
            this.InstallerInfo.Log.StartJob(base.DNN_Reading);
            ArrayList DnnErrors = this.ValidateDnn();
            if (DnnErrors.Count == 0)
            {
                IEnumerator refObjectHelperL0=null;
                this.InstallerInfo.Log.AddInfo(string.Format(base.DNN_Valid, "2.0"));
                PaFolderCollection Folders = new PaFolderCollection();
                XmlDocument doc = new XmlDocument();
                MemoryStream buffer = new MemoryStream(this.InstallerInfo.DnnFile.Buffer, false);
                doc.Load(buffer);
                this.InstallerInfo.Log.AddInfo(base.XML_Loaded);
                XmlElement FolderElement = (XmlElement) doc.DocumentElement.SelectSingleNode("folder");
                PaFolder Folder = new PaFolder();
                try
                {
                    Folder.Name = FolderElement.SelectSingleNode("name").InnerText.Trim();
                }
                catch (Exception exception1)
                {
                    
                    Exception ex = exception1;
                    throw new Exception(base.EXCEPTION_FolderName);
                    
                }
                try
                {
                    Folder.ProviderType = FolderElement.SelectSingleNode("type").InnerText.Trim();
                }
                catch (Exception exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    Exception ex = exception2;
                    throw new Exception(base.EXCEPTION_FolderProvider);
                    
                }
                this.InstallerInfo.Log.AddInfo(base.FILES_Loading);
                try
                {
                    refObjectHelperL0 = FolderElement.SelectNodes("files/file").GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        string name;
                        XmlElement file = (XmlElement) refObjectHelperL0.Current;
                        try
                        {
                            name = file.SelectSingleNode("name").InnerText.Trim();
                        }
                        catch (Exception exception3)
                        {
                            ProjectData.SetProjectError(exception3);
                            Exception ex = exception3;
                            throw new Exception(base.EXCEPTION_FileName);
                            
                        }
                        PaFile paf = (PaFile) this.InstallerInfo.FileTable[name.ToLower()];
                        if (paf == null)
                        {
                            this.InstallerInfo.Log.AddFailure(base.FILE_NotFound + name);
                        }
                        else
                        {
                            XmlElement pathElement = (XmlElement) file.SelectSingleNode("path");
                            if (pathElement != null)
                            {
                                string filepath = pathElement.InnerText.Trim();
                                this.InstallerInfo.Log.AddInfo(string.Format(base.FILE_Found, filepath, name));
                                paf.Path = filepath;
                            }
                            else
                            {
                                paf.Path = "";
                            }
                            Folder.Files.Add(paf);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                Folders.Add(Folder);
                if (!this.InstallerInfo.Log.Valid)
                {
                    throw new Exception(base.EXCEPTION_LoadFailed);
                }
                this.InstallerInfo.Log.EndJob(base.DNN_Success);
                return Folders;
            }
            try
            {
                refObjectHelperL1 = DnnErrors.GetEnumerator();
                while (refObjectHelperL1.MoveNext())
                {
                    string err = Conversions.ToString(refObjectHelperL1.Current);
                    this.InstallerInfo.Log.AddFailure(err);
                }
            }
            finally
            {
                if (refObjectHelperL1 is IDisposable)
                {
                    (refObjectHelperL1 as IDisposable).Dispose();
                }
            }
            throw new Exception(base.EXCEPTION_Format);
        }

        private ArrayList ValidateDnn()
        {
            ModuleDefinitionValidator ModuleValidator = new ModuleDefinitionValidator();
            MemoryStream xmlStream = new MemoryStream(this.InstallerInfo.DnnFile.Buffer);
            ModuleValidator.Validate(xmlStream);
            return ModuleValidator.Errors;
        }
    }
}

